<%@page language="java" pageEncoding="UTF-8"%>
<%@page import="com.shsh.safemanage.util.Util"%>
<%@page import="com.shsh.safemanage.model.ProblemTransferFeedback"%>
<%@page import="com.shsh.safemanage.dao.UserDao"%>
<%@page import="com.shsh.safemanage.dao.RoleDao"%>
<%@page import="java.util.ArrayList"%>
<%@page import="com.shsh.safemanage.model.Role"%>
<%@page import="com.shsh.safemanage.action.ActionCreator"%>
<%@page import="com.shsh.safemanage.action.UserAction"%>
<%@page import="com.shsh.safemanage.util.Constants"%>
<%@page import="com.shsh.safemanage.model.User"%>
<%@page import="com.shsh.safemanage.model.Problem"%>
<%@page import="java.util.List"%>
<%@page import="com.shsh.safemanage.dao.DaoCreator"%>
<%@page import="com.shsh.safemanage.dao.ProblemDao"%>

<%
{
ProblemDao theProblemDao = DaoCreator.createProblemDao();
RoleDao theRoleDao = DaoCreator.createRoleDao();
UserDao theUserDao = DaoCreator.createUserDao();
UserAction theUserAction = ActionCreator.createUserAction();

User theUser = (User) session.getAttribute(Constants.SESSION_USER);
Role theRole = theUserAction.getUserRole(theUser);

String dateStart = request.getParameter(Constants.REQUEST_PARAMETER_DATE_START);
String dateEnd = request.getParameter(Constants.REQUEST_PARAMETER_DATE_END);

List<Problem> problemList = (List<Problem>) session.getAttribute(Constants.SESSION_PROBLEM_LIST);
Integer totalNum = (Integer) session.getAttribute(Constants.SESSION_PROBLEM_NUMBER);

if (Util.isNullOrEmpty(dateStart)) {
	dateStart = (String) session.getAttribute(Constants.SESSION_DATE_START);
} else {
	session.setAttribute(Constants.SESSION_DATE_START, dateStart);
}

if (Util.isNullOrEmpty(dateEnd)) {
	dateEnd = (String) session.getAttribute(Constants.SESSION_DATE_END);
} else {
	session.setAttribute(Constants.SESSION_DATE_END, dateEnd);
}

if (Util.isNullOrEmpty(dateStart)) {
	dateStart = "";
}
if (Util.isNullOrEmpty(dateEnd)) {
	dateEnd = "";
}

if (!Util.isNullOrEmpty(dateStart) && !Util.isNullOrEmpty(dateEnd) && (problemList == null || totalNum == null)) {
	problemList = theProblemDao.getDepart1ProblemsTobeProcessed(theUser.getDepartment1ID(), Constants.PROBLEM_STATUS_CLOSE, dateStart, dateEnd);
	totalNum = theProblemDao.countDepartmentProblemNum(theUser.getDepartment1ID(), null, null, dateStart, dateEnd);
	
	session.setAttribute(Constants.SESSION_PROBLEM_LIST, problemList);
	session.setAttribute(Constants.SESSION_PROBLEM_NUMBER, totalNum);
}

List<Problem> chushiProblemList = new ArrayList<Problem>();
List<Problem> banziProblemList = new ArrayList<Problem>();
List<Problem> zhuangzhizhangZuzhangProblemList = new ArrayList<Problem>();
List<Problem> yixianProblemList = new ArrayList<Problem>();
List<Problem> zuyuanProblemList = new ArrayList<Problem>();

if ((theRole.getRoleType() & (Constants.ROLE_TYPE_BUZHANG | Constants.ROLE_TYPE_SHENGCHANFUBUZHANG | Constants.ROLE_TYPE_SHEBEIFUBUZHANG 
		| Constants.ROLE_TYPE_ANQUANZONGJIAN)) > 0) {
	int escalateProblemNum = 0;
	for (int i = 0; i < problemList.size(); i++) {
		Problem problem = problemList.get(i);
		if (problem.getWriterDepartmentType() == Constants.DEPARTMENT_TYPE_JIGUAN) {
			chushiProblemList.add(problem);
		} else {
			Role writerRole = theRoleDao.getRoleByUserID(problem.getWriteUserID());
			if ((writerRole.getRoleType() & (Constants.ROLE_TYPE_BUZHANG | Constants.ROLE_TYPE_SHENGCHANFUBUZHANG | Constants.ROLE_TYPE_SHEBEIFUBUZHANG
					| Constants.ROLE_TYPE_ANQUANZONGJIAN)) > 0) {
				banziProblemList.add(problem);
			} else if ((writerRole.getRoleType() & (Constants.ROLE_TYPE_ZHUANGZHIZHANG | Constants.ROLE_TYPE_ZUZHANG)) > 0) {
				zhuangzhizhangZuzhangProblemList.add(problem);
			} else if ((writerRole.getRoleType() & (Constants.ROLE_TYPE_YIXIANYUANGONG)) > 0) {
				yixianProblemList.add(problem);
			} else if ((writerRole.getRoleType() & (Constants.ROLE_TYPE_ZUYUAN)) > 0) {
				zuyuanProblemList.add(problem);
			}
		}
		
		if ((problem.getEscalateDepart1ID() != null && problem.getEscalateDepart1ID() > 0) || problem.isProcessingByChushi()) {
			escalateProblemNum++;
		}
	}
	
	int isZhenggaiNum = problemList.size();
	int isProcessingNum = escalateProblemNum;
	
	int zuoyebuRowSpan = 0;
	zuoyebuRowSpan += banziProblemList.size();
	zuoyebuRowSpan += zhuangzhizhangZuzhangProblemList.size();
	zuoyebuRowSpan += yixianProblemList.size();
	zuoyebuRowSpan += zuyuanProblemList.size();
%>
<div style="font-weight: bold; font-size: 13pt" align="center">
整个作业部共发现<span style="text-decoration: underline;"><%=totalNum%>个</span>问题，
仍在整改<span style="text-decoration: underline;"><%=isZhenggaiNum%>个</span>问题
（含上报职能处室<span style="text-decoration: underline;"><%=isProcessingNum%>个</span>问题）
</div>
<table align="center" border="1" width="99%" cellpadding="0" cellspacing="0" style="margin-top: 5px">
	<tr style="font-weight: bold" align="center">
		<td class="td1" colspan="2" width="10%">单位</td>
		<td class="td1" width="13%">作业部（业务中心）</td>
		<td class="td1" width="15%">装置</td>
		<td class="td1" width="10%">待整改问题标题</td>
		<td class="td1" width="22%">待整改问题内容</td>
		<td class="td1" width="5%">发现人</td>
		<td class="td1" width="10%">问题流转状态</td>
		<td class="td1" width="7%">查看</td>
		<td class="td1" width="8%">备注</td>
	</tr>
<%-- process chushi problem list --%>
<%
if (chushiProblemList.size() > 0) {
	Problem problem = chushiProblemList.get(0);
%>
	<tr>
		<td colspan="2" style="font-weight: bold" rowspan="<%=chushiProblemList.size()%>">职能处室</td>
		<td><%=problem.getDepartment1Name()%></td>
		<td><%=problem.getEquipmentName()%></td>
		<td><%=problem.getProblemTitle()%></td>
		<td><%=Util.isNullOrEmpty(problem.getProblemContent())? "&nbsp;":problem.getProblemContent(50)%></td>
		<td><%=problem.getWriteUserName()%></td>
		<td><%=theUserAction.getUserTransferFeedbackStr(theProblemDao.getLatestProblemTransferFeedbackByProblemID(problem.getId()))%></td>
		<td align="center">
		<a href="javascript:" 
			onclick="loadPageSection('<%=Problem.getTargetDetailedPageURL(problem, request.getContextPath())%>', 'mainContentDiv', {id:<%=problem.getId()%>,transferID:<%=problem.getTransferID()%>, fromPage: 'supervise'}, true)"
			style="color: <%=(problem.getProcessStatus() == Constants.PROBLEM_STATUS_CLOSE)? "black":"red"%>">详细内容</a>
		</td>
		<td><%=((problem.getEscalateDepart1ID() != null && problem.getEscalateDepart1ID() > 0) || problem.isProcessingByChushi())? "上报职能处室":"&nbsp;"%></td>
	</tr>
<%
	for (int i = 1; i < chushiProblemList.size(); i++) {
		problem = chushiProblemList.get(i);
%>
	<tr>
		<td><%=problem.getDepartment1Name()%></td>
		<td><%=problem.getEquipmentName()%></td>
		<td><%=problem.getProblemTitle()%></td>
		<td><%=Util.isNullOrEmpty(problem.getProblemContent())? "&nbsp;":problem.getProblemContent(50)%></td>
		<td><%=problem.getWriteUserName()%></td>
		<td><%=theUserAction.getUserTransferFeedbackStr(theProblemDao.getLatestProblemTransferFeedbackByProblemID(problem.getId()))%></td>
		<td align="center">
		<a href="javascript:" 
			onclick="loadPageSection('<%=Problem.getTargetDetailedPageURL(problem, request.getContextPath())%>', 'mainContentDiv', {id:<%=problem.getId()%>,transferID:<%=problem.getTransferID()%>, fromPage: 'supervise'}, true)"
			style="color: <%=(problem.getProcessStatus() == Constants.PROBLEM_STATUS_CLOSE)? "black":"red"%>">详细内容</a>
		</td>
		<td><%=((problem.getEscalateDepart1ID() != null && problem.getEscalateDepart1ID() > 0) || problem.isProcessingByChushi())? "上报职能处室":"&nbsp;"%></td>
	</tr>
<%
	}
}
%>

<%-- process zuoyebu problem list --%>
<%
if (zuoyebuRowSpan > 0) {
	List<List<Problem>> zuoyebuProblemList = new ArrayList<List<Problem>>();
	zuoyebuProblemList.add(banziProblemList);
	zuoyebuProblemList.add(zhuangzhizhangZuzhangProblemList);
	zuoyebuProblemList.add(yixianProblemList);
	zuoyebuProblemList.add(zuyuanProblemList);
	
	int listSelect = -1;
%>
<%-- the first row --%>
	<tr>
		<td rowspan="<%=zuoyebuRowSpan%>" style="font-weight: bold" width="5%">作业部</td>
		<%
		Problem problem = null;
		if (banziProblemList.size() > 0) {
			problem = banziProblemList.get(0);
			listSelect = 0;
		%>
		<td rowspan="<%=banziProblemList.size()%>">班子成员</td>
		<%
		} else if (zhuangzhizhangZuzhangProblemList.size() > 0) {
			problem = zhuangzhizhangZuzhangProblemList.get(0);
			listSelect = 1;
		%>
		<td rowspan="<%=zhuangzhizhangZuzhangProblemList.size()%>" width="5%">装置长（组长）</td>
		<%
		} else if (yixianProblemList.size() > 0) {
			problem = yixianProblemList.get(0);
			listSelect = 2;
		%>
		<td rowspan="<%=yixianProblemList.size()%>" width="5%">一线员工</td>
		<%
		} else if (zuyuanProblemList.size() > 0) {
			problem = zuyuanProblemList.get(0);
			listSelect = 3;
		%>
		<td rowspan="<%=zuyuanProblemList.size()%>" width="5%">组员</td>
		<%
		}
		%>
		<td><%=problem.getDepartment1Name()%></td>
		<td><%=problem.getEquipmentName()%></td>
		<td><%=problem.getProblemTitle()%></td>
		<td><%=Util.isNullOrEmpty(problem.getProblemContent())? "&nbsp;":problem.getProblemContent(50)%></td>
		<td><%=problem.getWriteUserName()%></td>
		<td><%=theUserAction.getUserTransferFeedbackStr(theProblemDao.getLatestProblemTransferFeedbackByProblemID(problem.getId()))%></td>
		<td align="center">
		<a href="javascript:" 
			onclick="loadPageSection('<%=Problem.getTargetDetailedPageURL(problem, request.getContextPath())%>', 'mainContentDiv', {id:<%=problem.getId()%>,transferID:<%=problem.getTransferID()%>, fromPage: 'supervise'}, true)"
			style="color: <%=(problem.getProcessStatus() == Constants.PROBLEM_STATUS_CLOSE)? "black":"red"%>">详细内容</a>
		</td>
		<td><%=((problem.getEscalateDepart1ID() != null && problem.getEscalateDepart1ID() > 0) || problem.isProcessingByChushi())? "上报职能处室":"&nbsp;"%></td>
	</tr>
<%-- the problem records of second row --%>
<%
	List<Problem> theProblemList = zuoyebuProblemList.get(listSelect);
	for (int i = 1; i < theProblemList.size(); i++) {
		problem = theProblemList.get(i);
%>
	<tr>
		<td><%=problem.getDepartment1Name()%></td>
		<td><%=problem.getEquipmentName()%></td>
		<td><%=problem.getProblemTitle()%></td>
		<td><%=Util.isNullOrEmpty(problem.getProblemContent())? "&nbsp;":problem.getProblemContent(50)%></td>
		<td><%=problem.getWriteUserName()%></td>
		<td><%=theUserAction.getUserTransferFeedbackStr(theProblemDao.getLatestProblemTransferFeedbackByProblemID(problem.getId()))%></td>
		<td align="center">
		<a href="javascript:" 
			onclick="loadPageSection('<%=Problem.getTargetDetailedPageURL(problem, request.getContextPath())%>', 'mainContentDiv', {id:<%=problem.getId()%>,transferID:<%=problem.getTransferID()%>, fromPage: 'supervise'}, true)"
			style="color: <%=(problem.getProcessStatus() == Constants.PROBLEM_STATUS_CLOSE)? "black":"red"%>">详细内容</a>
		</td>
		<td><%=((problem.getEscalateDepart1ID() != null && problem.getEscalateDepart1ID() > 0) || problem.isProcessingByChushi())? "上报职能处室":"&nbsp;"%></td>
	</tr>
<%
	}
%>
<%-- the left rows --%>
<%
	listSelect++;
	for (; listSelect < zuoyebuProblemList.size(); listSelect++) {
		theProblemList = zuoyebuProblemList.get(listSelect);
		String theRoleStr = "";
		if (listSelect == 1) {
			theRoleStr = "装置长（组长）";
		} else if (listSelect == 2) {
			theRoleStr = "一线员工";
		} else if (listSelect == 3) {
			theRoleStr = "组员";
		}
		
		if (theProblemList.size() > 0) {
			problem = theProblemList.get(0);
%>
	<tr>
		<td align="center" rowspan="<%=theProblemList.size()%>" width="5%"><%=theRoleStr%></td>
		<td><%=problem.getDepartment1Name()%></td>
		<td><%=problem.getEquipmentName()%></td>
		<td><%=problem.getProblemTitle()%></td>
		<td><%=Util.isNullOrEmpty(problem.getProblemContent())? "&nbsp;":problem.getProblemContent(50)%></td>
		<td><%=problem.getWriteUserName()%></td>
		<td><%=theUserAction.getUserTransferFeedbackStr(theProblemDao.getLatestProblemTransferFeedbackByProblemID(problem.getId()))%></td>
		<td align="center">
		<a href="javascript:" 
			onclick="loadPageSection('<%=Problem.getTargetDetailedPageURL(problem, request.getContextPath())%>', 'mainContentDiv', {id:<%=problem.getId()%>,transferID:<%=problem.getTransferID()%>, fromPage: 'supervise'}, true)"
			style="color: <%=(problem.getProcessStatus() == Constants.PROBLEM_STATUS_CLOSE)? "black":"red"%>">详细内容</a>
		</td>
		<td><%=((problem.getEscalateDepart1ID() != null && problem.getEscalateDepart1ID() > 0) || problem.isProcessingByChushi())? "上报职能处室":"&nbsp;"%></td>
	</tr>
<%
			for (int i = 1; i < theProblemList.size(); i++) {
				problem = theProblemList.get(i);
%>
	<tr>
		<td><%=problem.getDepartment1Name()%></td>
		<td><%=problem.getEquipmentName()%></td>
		<td><%=problem.getProblemTitle()%></td>
		<td><%=Util.isNullOrEmpty(problem.getProblemContent())? "&nbsp;":problem.getProblemContent(50)%></td>
		<td><%=problem.getWriteUserName()%></td>
		<td><%=theUserAction.getUserTransferFeedbackStr(theProblemDao.getLatestProblemTransferFeedbackByProblemID(problem.getId()))%></td>
		<td align="center">
		<a href="javascript:" 
			onclick="loadPageSection('<%=Problem.getTargetDetailedPageURL(problem, request.getContextPath())%>', 'mainContentDiv', {id:<%=problem.getId()%>,transferID:<%=problem.getTransferID()%>, fromPage: 'supervise'}, true)"
			style="color: <%=(problem.getProcessStatus() == Constants.PROBLEM_STATUS_CLOSE)? "black":"red"%>">详细内容</a>
		</td>
		<td><%=((problem.getEscalateDepart1ID() != null && problem.getEscalateDepart1ID() > 0) || problem.isProcessingByChushi())? "上报职能处室":"&nbsp;"%></td>
	</tr>
<%
			}
		}
	}
}
%>
</table>
<%
}
%>
<script type="text/javascript">
setElementValueByID('dateStart', '<%=dateStart%>');
setElementValueByID('dateEnd', '<%=dateEnd%>');
</script>
<%
}
%>

<div style="margin-top: 15px">
<%@include file="problems_depart1_statistic_section.jsp"%>
</div>
